package memberrewards.service;

import hackathon1212.ServletPojoAdapter;

import java.util.HashMap;
import java.util.Map;


import memberrewards.delegate.RewardsDelegate;
import memberrewards.dtos.AccountAdditionalInfoDTO;
import memberrewards.dtos.AccountDetailDTO;
import static memberrewards.helpers.Constants.BLANK;


public class AddAdditionalInfo extends ServletPojoAdapter {

	private static final long serialVersionUID = -8655087464272479429L;

	protected Object getBusinessMethod(Map<String, String[]> params){
		//Query Params
		final String pan =params.get("pan")!=null? params.get("pan")[0]:null;
		final String additionalInfoId = params.get("addinfoid") != null ? params.get("addinfoid")[0] : null;	
		final String additionalInfoDescription = params.get("addinfodes") != null ? params.get("addinfodes")[0] : null;	

		//Default response
		final Map<String,Object> ret = new HashMap<String, Object>();
		ret.put("status", "Failed to add Additional Info");
		if( (pan !=null && !BLANK.equals(pan)) && 
			(additionalInfoId !=null && !BLANK.equals(additionalInfoId))&& 
			(additionalInfoDescription!=null  && !BLANK.equals(additionalInfoDescription))
		){
			//Check if Primary Data Exists or Not
			final AccountDetailDTO accountDetail = RewardsDelegate.getAccountDetail(pan);	
			if(accountDetail!=null){
				AccountAdditionalInfoDTO accountAdditionalInfo = new AccountAdditionalInfoDTO();
				accountAdditionalInfo.setPan(pan);
				accountAdditionalInfo.setAdditionalInfoId(additionalInfoId);
				accountAdditionalInfo.setAdditionalInfoDescription(additionalInfoDescription);
				final boolean status=RewardsDelegate.addAccountAdditionalInfo(accountAdditionalInfo);
				System.out.println(getClass().getName()+" addAccountAdditionalInfo Status ::"+status);
				if(status){
					ret.put("status", "Successfully added Additional Info");
				}
				else{
					ret.put("status", "Failed to add Additional Info");
				}
			}
			else{
				ret.put("status", "Card Not Found");
			}
		}
		else{
			ret.put("status", "Any of the required input is either Null/Missing");
		}

		return ret;

	}
}
